<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\ApiController;
use App\Http\Requests\GroupRequest;
use App\Models\Group;
use App\Models\Package;
use Dotenv\Result\Success;
use Illuminate\Http\Request;

class GroupController extends ApiController
{

    public function index()
    {
        $groups = Group::all();
        return view('group.index', compact('groups'));
    }
    public function create(Group $group)
    {
        $packages = Package::all();
        return view('group.create', compact('packages', 'group'));
    }

    public function store(GroupRequest $request)
    {

        $group = Group::create($request->input());
        $group->packages()->sync($request->packages);
        return redirect()->route('admin.group.index')->with('success', '用户添加成功');
    }

    public function edit(Group $group)
    {
        $packages = Package::all();
        return view('group.edit', compact('packages', 'group'));
    }
    public function update(GroupRequest $request, Group $group)
    {
        $group->fill($request->input())->save();
        $group->packages()->sync($request->packages);
        return redirect()->route('admin.group.index')->with('success', '用户组修改成功');
    }

    public function destroy(Group $group)
    {
        $group->delete();
        return response()->json(['message' => '删除成功']);
    }
}
